/**
 * Copyright 2011, Under-Ground Studio.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.zju.vlis.aus.bean;

import java.util.Calendar;
import java.util.Date;


/**
 * Instance of this class represent an activity manager log record.
 * It interprets an activity manager log (with "-v time" format) to an object. 
 * 
 * @author ymsong (songyuming1985@gmail.com)
 * 2011-7-24
 */
public class LogRecord {
	// tag type.
	public static final int TAG_UNDEFINE = 0;
	public static final int TAG_CREATE = 1;
	public static final int TAG_RESTART = 2;
	public static final int TAG_RESUME = 3;
	public static final int TAG_PAUSE = 4;
	public static final int TAG_DESTROY = 5;
	
	private static final int TIME_STRING_LENGTH = 18;  
	
	private static final String CREATE = "am_create_activity";
	private static final String RESTART = "am_restart_activity";
	private static final String RESUME = "am_resume_activity";
	private static final String PAUSE = "am_pause_activity";
	private static final String DESTROY = "am_destory_activity";
	
	// time that occur
	private long time;
	// one of the tag types 
	private int tag;
	// package name
	private String packageName;
	// activity name
	private String activityName;

	/**
	 * Default constructor
	 */
	public LogRecord() {
		
	}
	
	/**
	 * Construct an instance by a log record. The record string must be the -v time 
	 * format of the logcat output. Format as:
	 * 07-24 13:17:07.020 I/am_restart_activity(   52): [1155045344,2,com.android.launcher/.Launcher]
	 * @param log
	 */
	public LogRecord(String log) {
		// set time
		String timeStr = log.substring(0, TIME_STRING_LENGTH);
		int month = Integer.parseInt(timeStr.substring(0, 2));
		int day = Integer.parseInt(timeStr.substring(3,5));
		int hour = Integer.parseInt(timeStr.substring(6, 8));
		int munite = Integer.parseInt(timeStr.substring(9, 11));
		int second = Integer.parseInt(timeStr.substring(12, 14));
		int milisec = Integer.parseInt(timeStr.substring(15, 18));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		calendar.set(Calendar.HOUR_OF_DAY,	hour);
		calendar.set(Calendar.MINUTE, munite);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND, milisec);
		time = calendar.getTimeInMillis();
		// set tag
		int tagBegin = log.indexOf("am_", TIME_STRING_LENGTH);
		int tagEnd = log.indexOf('(',tagBegin);
		String tagStr = log.substring(tagBegin, tagEnd);
		tagStr.trim();
		if (tagStr.equals(CREATE)){
			tag = TAG_CREATE;
		}
		else if (tagStr.equals(RESTART)){
			tag = TAG_RESTART;
		}
		else if (tagStr.equals(RESUME)){
			tag = TAG_RESUME;
		}
		else if (tagStr.equals(PAUSE)){
			tag = TAG_PAUSE;
		}
		else if (tagStr.equals(DESTROY))
		{
			tag = TAG_DESTROY;
		}
		//set package name
		int packageEnd = log.indexOf('/', tagEnd);
		int packageBegin = log.lastIndexOf(',', packageEnd);
		packageName = log.substring(packageBegin+1, packageEnd);
		//set activity name
		int activityEnd = log.indexOf(',', packageEnd);
		if (activityEnd == -1){
			activityEnd = log.indexOf(']', packageEnd);
		}
		activityName = log.substring(packageEnd+1, activityEnd);
	}

	/**
	 * @return the tag
	 */
	public int getTag() {
		return tag;
	}

	/**
	 * @param tag the tag to set
	 */
	public void setTag(int tag) {
		this.tag = tag;
	}

	/**
	 * @return the time
	 */
	public long getTime() {
		return time;
	}

	/**
	 * @param time
	 *            the time to set
	 */
	public void setTime(long time) {
		this.time = time;
	}

	/**
	 * @return the packageName
	 */
	public String getPackageName() {
		return packageName;
	}

	/**
	 * @param packageName
	 *            the packageName to set
	 */
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	/**
	 * @return the activityName
	 */
	public String getActivityName() {
		return activityName;
	}

	/**
	 * @param activityName
	 *            the activityName to set
	 */
	public void setActivityName(String activityName) {
		this.activityName = activityName;
	}
	
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("LogRecord:[");
		sb.append(packageName).append("/").append(activityName);
		sb.append(",event=");
		switch (tag)
		{
		case TAG_CREATE:
			sb.append("create");
			break;
		case TAG_RESTART:
			sb.append("restart");
			break;
		case TAG_RESUME:
			sb.append("resume");
			break;
		case TAG_PAUSE:
			sb.append("pause");
			break;
		case TAG_DESTROY:
			sb.append("destroy");
			break;
		}
		sb.append("]");
		return sb.toString();
	}
}
